import { AxesHelper, BoxGeometry, CircleGeometry, Mesh, MeshBasicMaterial, Scene, WebGLRenderer } from 'three'
import type { Camera } from 'three/src/cameras/Camera'
import type { Vector3 } from 'three/src/math/Vector3'

/**
 * 设置相机位置与视角
 * @param camera
 * @param position
 * @param lookAt
 */
export const setCamera = (
  camera: Camera,
  position: Vector3,
  lookAt: Vector3
) => {
  camera.position.setX(position.x)
  camera.position.setY(position.y)
  camera.position.setZ(position.x)
  camera.lookAt(lookAt)
}

export const createBox = () => {
  const geometry = new BoxGeometry(1, 1, 1)
  const material = new MeshBasicMaterial({ color: 'red' })
  const cube = new Mesh(geometry, material)

  return cube
}

export const createCircle = () => {
  const geometry = new CircleGeometry(5, 32)
  const material = new MeshBasicMaterial({ color: 'red' })
  const circle = new Mesh(geometry, material)

  return circle
}

/**
 * 创建辅助线
 * @param scene 场景对象
 * @param len 辅助线长度
 */
export const createHelpLine = (scene: Scene, len: number = 1000) => {
  const axHelp = new AxesHelper(len)
  scene.add(axHelp)
}

/**
 * 创建渲染器
 * @param element
 * @param size
 * @returns
 */
export const createRenderer = (element: HTMLElement, size: {width: number, height: number}) => {
  const renderer = new WebGLRenderer()
  const { width, height } = size

  renderer.setSize(width, height)
  element.appendChild(renderer.domElement)

  return renderer
}
